var $gridList=$('#grid');
var editDialog;
require(['grid', 'topBar', 'layout','date'], function () {
	var postData={};
	 $('body').layout();
    $('#top').topBar({
        left: [{
            type: 'filter-more',
            dateType:'custom',
            search: function () {
                if(getPostData()){
                    $('#grid').grid('setGridParam', {
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [ {
                   text: '时间快捷',
                   name1: 'beginTime',
                   name2: 'endTime',
                   type: 'dateLine'
               },
				{
				    text: '维保类型',
				    name: 'maintenCgy',
                    type: 'item-select',
                    dict: 'MAIN1000',
                    data: [
                           {text: '全部', value: 'All'}
                       ]
				}
            ]
        }],  
        right: [
            {
                text: '添加维保',
                type: 'btn',
                click: function () {
                	addCarMainteInfoDialog();
                }
            }
        ]
    });  

    $gridList.grid({
    	url : "/reportCarMainteDetail/getMaintenancePage.htm",
		multiselect:false,
		colModel: [
		           {name: "maintenId", index: "maintenId", label: 'maintenId', key: true,hidden: true},
		           {name: "licPltNo", index: "licPltNo", label: '车牌号' },
		           {name: "maintenCgy", index: "maintenCgy", label: '维保类别'},
		           {name: "maintenDate", index: "maintenDate", label: '维保日期'},
		           {name: "maintenMile", index: "maintenMile", label: '维保里程(km)'},
		           {name: "maintenWorkhours", index: "maintenWorkhours", label: '维保工时(小时)'},
		           {name: "opr", index: "opr", label: '维保人员'},
                   {name: "maintenFee", index: "maintenFee", label: '维保费用(元)'},
                   {name: "operate", width: 200, label: '操作', title: false,
	                   formatter: 'operArea',
	                   formatoptions: [{
	                       text: '编辑',
	                       css:'operate-edit',
	                       click: function (data) {
	                    	   getEditDialog({title : '编辑维保记录'}).dialog('exec', 'setVal',data.maintenId);
	                       }
	                   }, {
	                       text: '删除',
	                       css:'operate-delete',
	                       click: function (data) {
	                           $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
	                               if (r) {
	                               	$.ajaxExt({
	           							url : "/reportCarMainteDetail/delMaintenanceInfo.htm",
	           							data : {'maintenId' : data.maintenId}
	           						}).done(function(data){
	           							reloadGridData();
	           						});
	                               }
	                           });
	                       }
	                   }]
	           
	               }
               ]
    });
    
	//获取需要传递到后台的参数
	function getPostData(){
		var type = commonFun.getLeftTreeCurrentTreeType();
		if(type=='DRIVER'){
			type='CAR'
		}
		var carIds = commonFun.getLeftTreeCheckedNodes(type);
        if (carIds==null||!carIds.length) {
            $.messager.alert("提示", "请先选择左侧树的一辆车辆!", "warning");
            return false;
        }
       postData = $('#top').parent().getValues();
       postData.carIds=carIds.toString();
		return true;
	};
    
	// 弹出层控制-新增维保记录
	function addCarMainteInfoDialog() {
		getEditDialog({
			title : '新增维保记录',
			url:'./mainte/report/reportCarMainteInfoEdit.htm',
			height : 450,
			width : 700
		});
		getEditDialog().dialog('exec', 'reset');
	};
	
	function getEditDialog(option) {
		option = option || {};
		if (editDialog) {
			editDialog.dialog('open');
		if (option.title) {
			editDialog.dialog('setOption', 'title', option.title);
		}
		return editDialog;
	} else {
		editDialog = $('<div>').dialog(
				{
					title : option.title,
					url:'./mainte/report/reportCarMainteInfoEdit.htm',
					height : 450,
					width : 700,
					autoOpen : true,
					buttons : [
							{
								text : '保存',
								css : 'mr10',
								click : function() {
									$(this).dialog('exec', 'add',function(){
										getEditDialog().dialog('close');
										reloadGridData();
									});
								}
							}, {
								text : '取消',
								css : 'btn-cancel',
								click : function() {
									$(this).dialog('close');
									}
								} ]
					});
			 return editDialog;
		}
	}
	
	//重新查询维保信息grid
	var reloadGridData = function(){
		var carIds = commonFun.getLeftTreeCheckedNodes('CAR');
		postData = $('#top').parent().getValues();
		postData.carIds=carIds.toString();
		$gridList.grid('setGridParam', {
             datatype: "JSON",
             postData: postData
         }).trigger("reloadGrid");
	};
	
})
